import { createRouter, createWebHistory } from 'vue-router'
import request from '@/request/request'
const routes = [
  {
    path: '/login',
    name: '登陆',
    component: ()=>import('@/views/login/index'),
    redirect: '/loginnp',
    children: [
      {
        path: '/register',
        name: '注册',
        component: ()=>import('@/views/register/index'),
      },
      {
        path: '/loginnp',
        name: '电话或用户名登录',
        component: ()=>import('@/views/login/loginName_Phone')
      },
      {
        path: '/recoverpassword',
        name: '找回密码',
        component: ()=>import('@/views/login/recoverpassword')
      },
      {
        path: '/changepassword',
        name: '重置密码',
        component: ()=>import('@/views/login/changePassword')
      },


    ]
  },
  {
    path: '/',
    name: '仪表盘',
    component: ()=>import('@/views/index'),
    redirect: '/home',
    children:[
      {
        path: '/home',
        name: '首页',
        component: ()=>import('@/views/homeData/index'),
      }
    ]
  },
  {
    path: '/basic',
    name: '物品管理',
    component: ()=>import('@/views/index'),
    children:[
      {
        path: '/itemShow',
        name: '物品列表',
        component: ()=>import('@/views/basic/itemShow/index'),
      },
      {
        path: '/itemType',
        name: '物品类型',
        component: ()=>import('@/views/basic/itemType/index'),
      },
      {
        path: '/recordShow',
        name: '借出管理',
        component: ()=>import('@/views/basic/recordShow/index'),
      },
      {
        path: '/lookData',
        name: '数据展示',
        component: ()=>import('@/views/basic/lookData/index'),
      },
      {
        path: '/userManage',
        name: '用户管理',
        component: ()=>import('@/views/basic/userManage/index'),
      },
      {
        path:'/addQuestionnaire',
        name:'创建问卷',
        component:()=>import('@/views/basic/addQuestionnaire/index'),
      },
      {
        path:'/showQuestionnaire',
        name:'查看问卷',
        component:()=>import('@/views/basic/showQuestionnaire/index'),
      },
      {
        path:'/showQuestionnaireResult',
        name:'查看问卷结果',
        component:()=>import('@/views/basic/showQuestionnaireResult/index'),
      }
    ],
  },
  {
    path: '/sys',
    name: '权限设置',
    component: ()=>import('@/views/index'),
    children:[
      {
        path: '/auth',
        name: '权限管理',
        component: ()=>import('@/views/sys/auth/index'),
      },
      {
        path: '/role',
        name: '角色管理',
        component: ()=>import('@/views/sys/role/index'),
      },
      {
        path: '/mans',
        name: '人员管理',
        component: ()=>import('@/views/sys/mans/index'),
      },
      {
        path: '/loginMan',
        name: '用户登录权限管理',
        component: ()=>import('@/views/sys/loginMan')
      }
    ],
  },
  {
    path: '/sett',
    name: '系统设置',
    component: ()=>import('@/views/index'),
    children:[
      {
        path: '/pwd',
        name: '修改密码',
        component: ()=>import('@/views/sys/updatePwd/index'),
      },
      {
        path: '/perPwd',
        name: '个人密码',
        component: ()=>import('@/views/sys/perPwd/index'),
      }
    ]
  },
  {
    path: '/group',
    name: '群组管理',
    component: ()=>import('@/views/index'),
    children: [
      {
        path: '/userGru',
        name: '用户群组管理',
        component: ()=>import('@/views/sys/group/userGroup')
      },
      {
        path: '/answerGru',
        name: '答者群组管理',
        component: ()=>import('@/views/sys/group/answerGroup')
      },{
         path: '/userGruChan',
        name: '修改用户群组信息',
        component: ()=>import('@/views/sys/group/userGroupChan')
      },{
      path: '/creatUserGroup',
        name: '创建新的用户群组',
        component: ()=>import('@/views/sys/group/creatUserGroup')
      },{
        path: '/creatAnswerGroup',
        name: '创建新的答者群组',
        component: ()=>import('@/views/sys/group/creatAnswerGroup')
      },{
        path: '/answerGruChan',
        name: '修改答者群组信息',
        component: ()=>import('@/views/sys/group/answerGroupChan')
      }

    ]
  },
  {
    path:'/previewQuestionnaire',
    name:'预览',
    component:()=>import('@/views/basic/AnswerQuestionnaire/preview.vue')
  },
  {
    path:'/Answererlogin/:id',
    name:'答者登录',
    component:()=>import('@/views/login/AnswererLogin.vue'),
    children: [

    ]
  },
  {
    path:'/AnswerQuestionnaire',
    name:'回答',
    component:()=>import('@/views/basic/AnswerQuestionnaire/answer.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path=='/home'){ //验证是否需要登陆
    let token = localStorage.getItem('token')
    let logindto = {}
    logindto.token=token
    logindto.urlpath=to.path
    if(token!=null){ //登录过
      //验证token
      request.post("/user/checkToken",token).then(res=>{
        //成功
        if(res.statusCode == '200'){
          next({path:'/login'})
          // if(to.path=='/' || to.path=='/home'){
          //   next()
          // }else {
          //   request.post("/user/checkURL",logindto).then(res=>{
          //     if(res.statusCode == '200'){
          //       next()
          //     }else{
          //       next({path:'/login'})
          //     }
          //   })
          // }

        }else{
          next()
        }

      }).catch(()=>{
        next({path:'/login'})
      })

    }else{ //没有登录
      next({path:'/login'})
    }
  }

  return next()
})

export default router
